package lesson17;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class MyThreadLocal2 {

    private static ExecutorService executorService = Executors.newFixedThreadPool(10);

    static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");

    public static void main(String[] args) throws InterruptedException {
        /*new Thread(() ->{
            System.out.println(new MyThreadLocal2().date(1));
        }).start();
        Thread.sleep(1000);
        new Thread(() ->{
            System.out.println(new MyThreadLocal2().date(2));
        }).start();*/
        /*for(int i = 0; i < 10; i++){
            int finalI = i;
            new Thread(() ->{
                System.out.println(new MyThreadLocal2().date(finalI));
            }).start();
            Thread.sleep(100);
        }*/
        for(int i = 0; i < 20; i++){
            int finalI = i;
            executorService.execute(new Thread(() ->{
                System.out.println(new MyThreadLocal2().date(finalI));
            }));
            //Thread.sleep(100);
        }
    }

    public String date(int seconds){
        Date date = new Date(1000 * seconds);
        SimpleDateFormat simpleDateFormat = ThreadSafeFormat.dateFormatThreadLocal.get();
        //System.out.println(System.identityHashCode(simpleDateFormat));
        return simpleDateFormat.format(date);
    }

    static class ThreadSafeFormat{
        public static ThreadLocal<SimpleDateFormat> dateFormatThreadLocal = ThreadLocal.withInitial(() -> new SimpleDateFormat("mm:ss"));
    }

}
